package service;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import robocode.control.BattleSpecification;
import robocode.control.BattlefieldSpecification;
import robocode.control.RobocodeEngine;
import robocode.control.RobotSpecification;
import robot.RobotAgent;

import com.agentfactory.logic.lang.FOS;
import com.agentfactory.platform.core.IAgent;
import com.agentfactory.platform.impl.AbstractPlatformService;

public class RobotAgentService extends AbstractPlatformService{

    protected List<RobotAgent> listOfRobotAgentsCreated = new ArrayList<RobotAgent>();
    public Map<String, RobotAgent> mapAgentToRobotAgent = new HashMap<String, RobotAgent>();
    public Map<RobotAgent, String> mapRobotAgentToAgent = new HashMap<RobotAgent, String>();
    public Map<String, List<FOS>> mapAgentToPercepts = new HashMap<String, List<FOS>>();
	private RobocodeEngine roboCodeEngine;
	private BattleSpecification battleSpec;
	
	@Override
	protected void onStart() {
		// File temp = new File("c:/robocode") ;
		// System.out.println(temp.canRead()+":"+temp.canWrite()+":"+temp.canExecute());
    	roboCodeEngine = new RobocodeEngine(new File("c:/robocode"));
    	// RobocodeEngine.addBattleListener(new BattleObserver());
    	roboCodeEngine.setVisible(true);
    	
    	RobotSpecification[] robotSpec = roboCodeEngine.getLocalRepository("robot.RobotAgent,robot.RobotAgent,robot.RobotAgent,robot.RobotAgent,robot.RobotAgent,sample.Fire");
    	battleSpec = new BattleSpecification(1, new BattlefieldSpecification(1200,1000), robotSpec);
	}
	
	protected void onBind(IAgent agent) {
		if(!this.listOfRobotAgentsCreated.isEmpty()){
			RobotAgent test = this.listOfRobotAgentsCreated.get(0) ;
            mapRobotAgentToAgent.put(test, agent.getName());
            mapAgentToRobotAgent.put(agent.getName(), listOfRobotAgentsCreated.remove(0));
            mapAgentToPercepts.put(agent.getName(), new LinkedList<FOS>());
		}else{
			System.out.println("I have no more robots to bind an agent to it, sorry!");
		}
	}
    public List<FOS> getPercepts(IAgent agent) {
        return mapAgentToPercepts.get(agent.getName());
    }

    public List<FOS> getPercepts(String name) {
        return mapAgentToPercepts.get(name);
    }
    
    @Override
    public void onUnbind(IAgent agent) {
        // Enter Agent Unbind Code Here
    }

    @Override
    public void onStop() {
        // Enter Stop Code Here
    }
    
    
    public RobotAgentService() {
    	listOfRobotAgentsCreated = new ArrayList<RobotAgent>();
    }
    
    public void addRobotAgent(RobotAgent robot){
    	this.listOfRobotAgentsCreated.add(robot);
    	//Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Added robot reference: "+robot.getName()+" Size of list: "+this.listOfRobotAgentsCreated);
    	System.out.println("added robot to list");
    }
    public void addRobotAgent(RobotAgent[] robotAgents){
    	for(int i = 0; i < robotAgents.length; i++){
    		this.listOfRobotAgentsCreated.add(robotAgents[i]);
    	}
    	System.out.println("added robot array to list");
    }


	public void startBattle(){
		roboCodeEngine.runBattle(this.battleSpec, true);
	}
	
    public RobotAgent lookupRobotAgent(String agentName){
		
    	return this.mapAgentToRobotAgent.get(agentName);
    	
    }
    
    /*
    private void updatePercepts(String agent) {
        List<FOS> percepts = mapAgentToPercepts.get(agent);
    	percepts.clear();

    	RobotAgent spec = lookupRobotAgent(agent);
    	// Agents that are not vacBots cannot have any percepts
    	if (spec == null) return;

        percepts.add(new FOS("location(" + spec.getX() + "," + spec.getY() + ")"));
    }*/
}
